package com.example.dlg;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textview1);

        Button button2 = findViewById(R.id.btn2);
        Button button3 = findViewById(R.id.btn3);
        Button button4 = findViewById(R.id.btn4);
        // 列表对话框
        Button btn5 = findViewById(R.id.btn5);
        Button btn6 = findViewById(R.id.btn6);

        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);

    }

    private void showDatePickerDialog() {
        // 创建 DatePickerDialog 实例
        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                // 获取选定的日期
                int selectedDay = dayOfMonth;
                int selectedMonth = month + 1; // 月份从 0 开始，所以需要加 1
                int selectedYear = year;

                // 更新日期显示
                updateDateDisplay(selectedDay, selectedMonth, selectedYear);
            }
        },  2023, 9, 1); // 设置初始选定的日期为当前日期
        // 显示 DatePickerDialog
        datePickerDialog.show();
    }

    private void updateDateDisplay(int selectedDay, int selectedMonth, int selectedYear) {
        // 这里可以根据需要更新界面上的日期显示，例如更新一个 TextView 的文本内容
        textView.setText(selectedYear + "-" + selectedMonth + "-" + selectedDay);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id == R.id.btn2){
            // 弹出一个提醒对话框
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("标题");
            builder.setMessage("这是一条消息");
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // 处理点击事件
                    Toast.makeText(MainActivity.this,"取消按钮被点击了",Toast.LENGTH_SHORT).show();
                }
            });

            builder.setNeutralButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this,"确定按钮被点击了",Toast.LENGTH_SHORT).show();
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }else if(id == R.id.btn3){
            // 弹出一个提醒对话框
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//                builder.setTitle("标题");
//                builder.setMessage("这是一条消息");
            builder.setItems(new String[]{"选项1", "选项2", "选项3"}, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // 处理点击事件
                    switch (which) {
                        case 0:
                            // 点击了 "选项1"
                            Toast.makeText(MainActivity.this,"选项1",Toast.LENGTH_SHORT).show();
                            break;
                        case 1:
                            // 点击了 "选项2"
                            Toast.makeText(MainActivity.this,"选项2",Toast.LENGTH_SHORT).show();
                            break;
                        case 2:
                            // 点击了 "选项3"
                            Toast.makeText(MainActivity.this,"选项3",Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }else if(id == R.id.btn4){
            // 弹出一个提醒对话框
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setMultiChoiceItems(new String[]{"选项1", "选项2", "选项3"}, new boolean[]{false,false,false}, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    switch (which) {
                        case 0:
                            // 点击了 "选项1"
                            Toast.makeText(MainActivity.this,"选项1",Toast.LENGTH_SHORT).show();
                            break;
                        case 1:
                            // 点击了 "选项2"
                            Toast.makeText(MainActivity.this,"选项2",Toast.LENGTH_SHORT).show();
                            break;
                        case 2:
                            // 点击了 "选项3"
                            Toast.makeText(MainActivity.this,"选项3",Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            });
            builder.setNeutralButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }else if(id == R.id.btn5){
            // 弹出一个提醒对话框
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("列表对话框");
            String[] colors = new String[]{"红色","黄色","蓝色"};
            builder.setItems(colors,new ItemOnClickListener());
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }else if(id == R.id.btn6){
            // 弹出一个提醒对话框
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("列表对话框");
            String[] colors = new String[]{"红色","黄色","蓝色"};
            builder.setSingleChoiceItems(colors,0,new ItemOnClickListener());
            builder.setPositiveButton("确定",new ItemOnClickListener());
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }
    }


    class ItemOnClickListener implements DialogInterface.OnClickListener{

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
                case 0:
                    Toast.makeText(MainActivity.this,"红",Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Toast.makeText(MainActivity.this,"黄",Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    Toast.makeText(MainActivity.this,"蓝",Toast.LENGTH_SHORT).show();
                    break;
                default:
                    Toast.makeText(MainActivity.this,"红",Toast.LENGTH_SHORT).show();
            }

        }
    }


}